import { SOUNDS } from "../../../../../core/sound/interface";
import { getAttrOrOther, keepDecimal, randomChoice } from "../../../../../core/utils/utils";
import { attrDefine } from "../../../../defines/attr_define";
import { damageCalc, getDamage } from "../../../../defines/formula";
import { LimbsTypeDefine } from "../../../../defines/people_limbs";
import { Skill } from "../../../skill";

export default class SkillNode extends Skill{
  constructor(root, num){
    super(root, num);
  }

  getName(){
    let limb = this.getBattleData('limb');
    if(limb) return limb.text + '击'

    return '普通攻击';
  }

  getDesc(){
    return `使用自身肢体进行攻击。`
  }

  getTags(){
    return [
      { text: '气血', type: 'error' },
    ]
  }

  getTargets(){
    let battleUnits = this.root.battleMgr.getOtherUnits(this.root.battleMgr.actionUnit.type);

    return [randomChoice(battleUnits)];
  }

  getData(){
    let actionUnit = this.root.battleMgr.actionUnit;

    return new Promise(resolve => {
      let limbs = actionUnit.people.getLimbs(this.getNeedLimbs())

      let { optionSelected } = this.root.gameState.dialogs.showOptionChooser('选择肢体', {
        active: false,
        options: limbs.map(item => ({
          value: item,
          text: item.text || LimbsTypeDefine[item.key].text,
          desc: '效果转化: ' + keepDecimal(item.$power * 100, 0) + '%',
        }))
      })

      optionSelected.add(limb => {
        resolve({ limb })
      })
    })
  }

  getAiData(){
    let actionUnit = this.root.battleMgr.actionUnit;

    return {
      limb: actionUnit.people.getLimbs().sort((a, b) => {
        return b.$power - a.$power;
      })[0]
    }
  }

  runSkill(){
    let actionUnit = this.root.battleMgr.actionUnit;
    let target = actionUnit.action.targets[0];

    let limb = actionUnit.action.data.limb;

    let damage = getDamage({
      atkType: attrDefine.blood.key,
      attackPeopleAttr: actionUnit.people.data.attr,
      defPeopleAttr: target.people.data.attr,
      power: limb.$power,
    })

    target.people.data.useAttr.hp -= damage.val;
    target.showText(-damage.val, 'danger');
    this.root.app.sound.playUiSound(SOUNDS.basicAtk)
  }
  
  runEffect(){
    return new Promise(resolve => {
      setTimeout(() => {
        resolve();
      }, 500);
    })
  }
}